<template>
  <div class="columnBox">
    <el-row>
      <el-col :span="19"><div class="grid-content"></div></el-col>
      <el-col :span="2"
        ><div class="grid-content save_sort" @click="saveSort">
          <i class="el-icon-s-check"></i>保存排序
        </div></el-col
      >
      <el-col :span="3"
        ><div class="grid-content">
          <el-button type="primary" @click="toAdd"
            ><i class="el-icon-plus"></i>新增栏目</el-button
          >
        </div></el-col
      >
    </el-row>

    <!-- 内容区域 -->
    <el-table
      :data="list"
      v-loading="loading"
      style="width: 100%; margin-bottom: 20px"
      row-key="columnId"
      :tree-props="{ children: 'columnManagements', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="栏目名称" prop="columnName"></el-table-column>
      <el-table-column label="路径" prop="columnPath" width="140px"> </el-table-column>
      <el-table-column label="导航栏状态" width="140px">
          <template slot-scope="props">
              <span> {{props.row.columnState | getState}} </span>
          </template>
      </el-table-column>
      <el-table-column label="导航栏类型" prop="columnType"> </el-table-column>
      <el-table-column label="排序" width="100px">
          <template slot-scope="props">
              <el-input v-model="props.row.columnSort" value="props.row.columnSort" @change="insert(props.row)"></el-input>
          </template>
      </el-table-column>
      <el-table-column label="操作">
          <template slot-scope="props">
              <span class="manage" @click="toColumnSet(props.row)">栏目设置</span> <i></i>
              <span class="manage" @click="toColumnContent(props.row)">栏目内容</span> <i></i>
              <span class="manage" @click="deleteColumn(props.row)">删除</span>
          </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      changeList: [],
      loading: true
    };
  },
  filters: {
      getState(val) {
          return val == 0 ? '不显示' : '显示'
      }
  },
  methods: {
      toColumnSet(val) {
        // 保存当前id及parentId
        console.log(val);
        this.$store.commit('setColumnId',val.columnId)
        this.$store.commit('setColumnParent',val.columnFatherId)
        this.$router.push(`/columnSet/${val.columnId}/${val.columnFatherId}`)
      },
      toColumnContent(data){
        this.$store.commit('setColumnId',data.columnId)
        this.$router.push(`/columnContent/${data.columnId}`)
      },
      async initColumn() {
        let { data: { data } } = await this.$http.getColumn()
        // console.log(data);
        this.list = data
        this.loading = false
      },
      insert(data) {
        delete data.columnManagements
        this.changeList.push(data)
      },
      async saveSort() {
        this.$confirm("是否保存当前排序?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let params = JSON.stringify(this.changeList)
          let { meta: {status} } = await this.$http.getModify(params)
          if (status == 200) {
            this.$message({
              type: "success",
              message: "排序已保存!",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "保存失败",
          });
          this.initColumn()
        });
      },
      toAdd() {
        this.$router.push('/addColumn')
      },
      async deleteColumn(data) {
        this.$confirm("此操作将永久删除该栏目,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
            // console.log(data.columnId);
            let { meta: { status, msg } } = await this.$http.getDeleteColumn(data.columnId)
            if (status == 200) {
              this.$message({
                type: "success",
                message: msg,
              });
              this.initColumn()
            }
        })
        .catch(() => {
          this.$message({
            type: "warning",
            message: "取消操作",
          });
        });

      }
  },
  created() {
    this.initColumn()
  }
};
</script>

<style scoped lang="less">
.columnBox {
  width: 100%;
  min-width: 1200px;
  box-sizing: border-box;
  padding-left: 70px;
  padding-right: 70px;
}
.el-row {
  .el-col {
    height: 60px;
    .grid-content {
      line-height: 60px;
      color: #108ee9;
      font-size: 14px;
      font-family: "Microsoft YaHei ", "Microsoft YaHei", sans-serif;
      .el-button {
        padding: 8px 14px;
      }
    }
  }
}
/deep/.el-table_1_column_3,/deep/.el-table_1_column_2 {
    color: rgba(0, 0, 0, 0.301);
}
/deep/.el-table__header-wrapper .cell {
    color: #000;
}
.el-table__cell {
    color: #ccc;
    .manage {
        margin: 0 5px;
        color: #108ee9;
        cursor: pointer;
    }
    i {
        display: inline-block;
        width: 1px;
        height: 10px;
        background-color: #ccc;
    }
}
/deep/.el-input__inner {
    width: 50px;
    height: 32px !important;
    padding-left: 6px;
    padding-right: 6px;
    margin-left: -10px;
}
.save_sort {
  cursor: pointer;
}
</style>
